package com.example.graduationdesignbackend.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.entity.ProductOrder;
import com.example.graduationdesignbackend.exception.ParkException;
import com.example.graduationdesignbackend.mapper.ProductOrderMapper;
import com.example.graduationdesignbackend.query.ProductOrderQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.IProductOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuze
 * @since 2024-05-13
 */
@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements IProductOrderService {

  @Override
  public void createProductOrder(ProductOrder productOrder) {
    ProductOrder order = productOrder;
    order.setId(RandomUtil.randomNumbers(9));
    order.setStatus(0);
    order.setCreateTime(new Date().getTime());
    Boolean save = save(order);
    if (!save) throw new ParkException(ResponseCode.SaveError);
  }

  @Override
  public PageDto<ProductOrder> getMyProductOrderPage(ProductOrderQuery productOrderQuery) {
    String key = productOrderQuery.getKey();
    int current = 1;
    if (!BeanUtil.isEmpty(productOrderQuery.getCurrent())){
      current = productOrderQuery.getCurrent();
    }
    int pageSize = 10;
    if (!BeanUtil.isEmpty(productOrderQuery.getPageSize())){
      pageSize = productOrderQuery.getPageSize();
    }
    Page<ProductOrder> page = Page.of(current,pageSize);
    page.addOrder(new OrderItem("create_time",false));
    Page<ProductOrder> p = new Page<>();
    if (BeanUtil.isEmpty(productOrderQuery.getKey())){
      p = lambdaQuery()
        .eq(ProductOrder::getUserId, StpUtil.getLoginIdAsString())
        .page(page);
    }else {
      p = lambdaQuery()
        .eq(ProductOrder::getUserId,StpUtil.getLoginIdAsString())
        .like(ProductOrder::getProductTitle,key)
        .page(page);
    }
    PageDto<ProductOrder> dto = new PageDto<>();
    dto.setTotal(p.getTotal());
    dto.setPages(p.getPages());
    dto.setPageSize(p.getSize());
    dto.setCurrent(p.getCurrent());
    dto.setList(p.getRecords());
    return dto;
  }

  @Override
  public PageDto<ProductOrder> getProductOrderPage(ProductOrderQuery productOrderQuery) {
    String key = productOrderQuery.getKey();
    int current = 1;
    if (!BeanUtil.isEmpty(productOrderQuery.getCurrent())){
      current = productOrderQuery.getCurrent();
    }
    int pageSize = 10;
    if (!BeanUtil.isEmpty(productOrderQuery.getPageSize())){
      pageSize = productOrderQuery.getPageSize();
    }
    Page<ProductOrder> page = Page.of(current,pageSize);
    page.addOrder(new OrderItem("create_time",false));
    Page<ProductOrder> p = new Page<>();
    if (BeanUtil.isEmpty(productOrderQuery.getKey())){
      p = lambdaQuery()
        .page(page);
    }else {
      p = lambdaQuery()
        .like(ProductOrder::getProductTitle,key)
        .page(page);
    }
    PageDto<ProductOrder> dto = new PageDto<>();
    dto.setTotal(p.getTotal());
    dto.setPages(p.getPages());
    dto.setPageSize(p.getSize());
    dto.setCurrent(p.getCurrent());
    dto.setList(p.getRecords());
    return dto;
  }

  @Override
  public Res handleProductOrder(String id) {
    ProductOrder productOrder = getById(id);
    productOrder.setStatus(9);
    productOrder.setUpdateTime(new Date().getTime());
    Boolean update = updateById(productOrder);
    if (!update) throw new ParkException(ResponseCode.UpdateError,"完成订单失败");
    return Res.yuze();
  }

  @Override
  public Res getSalesVolume() {
    List<ProductOrder> productOrderList = lambdaQuery().list();
    List<ProductOrder> list = new ArrayList<>();
    for(int i=0; i<productOrderList.size();i++){
      Boolean x = true;
      for(int item=0;item<list.size();item++){
        if (list.get(item).getProductId().equals(productOrderList.get(i).getProductId())){
          list.get(item).setBuyNumber(list.get(item).getBuyNumber()+productOrderList.get(i).getBuyNumber());
          x=false;
        }
      }
      if (x){
        list.add(productOrderList.get(i));
      }
    }
    List<ProductOrder> list1 = new ArrayList<>();
    list.sort(Comparator.comparing(ProductOrder::getBuyNumber).reversed());
    if (list.size()<4){
        list1.addAll(list);
    }else{
      list1.add(list.get(0));
      list1.add(list.get(1));
      list1.add(list.get(2));
    }
    return Res.yuze(list1);
  }
}
